<template>
	<div class="title" @click="comeBack()">
			<image src="../../static/icon_arrow_blue_left.png"></image>
			报告查询
		</div>
	<div class="report_content" id="reportDetail" >

		<div class="report_content_info">
			<div>{{info['PATI']['ROW'][0]['XM'] }}</div>
			<div>{{info['PATI']['ROW'][0]['XB'] }}</div>

		</div>
		<div class="report_content_info">
			<div>{{info['PATI']['ROW'][0]['NL'] }}</div>
			<div>{{info['PATI']['ROW'][0]['BB']}}</div>
		</div>

	</div>
	<div style="width: 80%;margin: auto;line-height: 60px;" class="">报告详情</div>
	<view class="particulars" v-if="list">
		<view class="particulars-S" v-for="(item,index) in list['TABLE']['ROW']" :key="index">
			<view class="particulars-S" v-if="Array.isArray(item.COL)">
				<view class="top-left">
					{{!item.COL[1]?'暂无数据':item.COL[1] ||''}}
				</view>
				<view class="top-content">
					<view class="a">
						{{!item.COL[2]?'暂无数据':item.COL[2] || ''}}
					</view>
					<view class="b">
						参考值：{{!item.COL[4]?'暂无参考值':item.COL[4]}}
					</view>
				</view>

			</view>
			<view v-if="!Array.isArray(item.COL)">
				<view class="top-left">
					{{!item.COL?'暂无数据':item.COL}}
				</view>
			</view>
		</view>
	</view>
	<view class="Reportdetai" v-if="list">
		<view class="Reportdetai-a">
			<view class="Reportdetai-right">
				{{list['PATI']['ROW'][1].KS || ''}}
			</view>
		</view>

		<view class="Reportdetai-a">
			<view class="Reportdetai-right">
				{{list['PATI']['ROW'][1].LX || ''}}
			</view>
		</view>

		<view class="Reportdetai-a">
			<view class="Reportdetai-right">
				{{a || ''}}
			</view>

		</view>
		<view class="Reportdetai-a">
			<view class="Reportdetai-right">
				{{b || ''}}
			</view>
		</view>
		<view class="Reportdetai-a">
			<view class="Reportdetai-right">
				{{c || ''}}
			</view>
		</view>
		<view class="Reportdetai-a" v-if="Array.isArray(list['INFO']['ROW'])">
			<view class="Reportdetai-right">
				{{list['INFO']['ROW'][1]}}:{{list['INFO']['ROW'][2]}}
			</view>
		</view>
		<view class="Reportdetai-a" v-if="!Array.isArray(list['INFO']['ROW'])">
			<view class="Reportdetai-right">
				{{list['INFO']['ROW'] || ''}}
			</view>
		</view>
	</view>


	<view style="height: 20rpx;">

	</view>

<!--	<div class="downloadButton" @click="download()">下载报告</div>-->
	<div class="downloadButton" @click="text.onClick">下载报告</div>
</template>




<script lang="ts">
const printerModule = uni.requireNativePlugin("PrinterModule")
	import { request } from '../../Providing/request';
	import { networkInterface } from '../../globalVariable/networkInterface';
	import { uiCollection } from '../../Providing/uiCollection';
import { globalVariable } from '../../globalVariable/globalVariable';
	export default {
		data() {
			return {

				info: {},
				list: {},
				a: "",
				b: '',
				c: '',
                posterUrl: "",
            }
		},

		methods: {
            // 获取生成的base64 图片路径并执行打印
            receiveRenderData(val) {
                let posterUrl = val.replace(/[]/g, ''); // 去除base64位中的空格
                console.log('start',posterUrl)
                this.posterUrl = posterUrl;
                setTimeout(()=>{
                    this.handlePrint()
                })
            },
            /**打印*/
            handlePrint() {
                // 添加打印资源
                const result = printerModule.addPrintFile({
                    fileBase64: this.posterUrl
                })
                if (result.success) {
                    uni.showToast({
                        title: '添加打印资源成功!',
                        icon: 'success',
                        duration: 2000
                    });
                    setTimeout(() => {
                        uni.hideToast();
                    }, 2000);
                } else {
                    uni.showToast({
                        title: '添加打印资源失败!',
                        icon: 'none',
                        duration: 2000
                    });
                    setTimeout(() => {
                        uni.hideToast();
                    }, 2000);
                }
                //开始打印
                const printResult = printerModule.startPrint();
                if (printResult.success) {
                    uni.showToast({
                        title: '打印成功!',
                        icon: 'success',
                        duration: 2000
                    });
                    setTimeout(() => {
                        uni.hideToast();
                    }, 2000);
                }else{
                    uni.showToast({
                        title: '打印失败!',
                        icon: 'none',
                        duration: 2000
                    });
                    setTimeout(() => {
                        uni.hideToast();
                    }, 2000);
                }
            },
			comeBack() {
				uni.$emit('goToCaseSearch')
			},
			getRecordXmlDetailJYQuery() {
				new request().getRecordXmlDetailJYQuery(globalVariable.reportDetailsId).then((res) => {
					if(res['ZLEPR'] && JSON.stringify(res['ZLEPR'] )!== "{}"){
						this.info=res['ZLEPR'];
						this.list = res['ZLEPR'];
						console.log("报告数据", this.list)
						this.list['TABLE']['ROW'].shift()
						const a = this.list['INFO']['ROW'][0].replace(/\$/g, "")
						this.a = a.slice(5, 20)
						this.b = a.slice(20, 28)
						this.c = a.slice(28, 36)

					}else{
						uiCollection.showToast("获取报告详情失败");
						uni.$emit("goToHome");
					}


				}).catch((err)=>{
					uiCollection.showToast("获取报告详情失败");
					uni.$emit("goToHome");
				})

			},
			download() {
				console.log('开始下载报告')

				let dtask = plus.downloader.createDownload(networkInterface.downloadPdf + "/"+globalVariable.reportDetailsId, {},
					function (d, status) {
						if (status == 200) {
							console.log("下载返回内容", d);
							uiCollection.showToast("文件下载成功")
							//下载成功,d.filename是文件在保存在本地的相对路径，使用下面的API可转为平台绝对路径
							let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
							// plus.runtime.openFile(d.filename); //选择软件打开文件
							console.log('打印', fileSaveUrl)
							const plugin = uni.requireNativePlugin('hpPrintShare');

							plugin.sayHi(fileSaveUrl,  (res)=> {
								uni.$emit("goToHome");
							})

						} else {
							//下载失败
							uiCollection.showToast("文件下载失败");

							plus.downloader.clear(); //清除下载任务
						}
					})

				dtask.start();
			}
		},
		comeBack(){
				uni.$emit('goToCaseSearch')
		},
		created() {
			this.getRecordXmlDetailJYQuery()
		},
	}
</script>
<!-- renderjs的script标签 -->

<script module="text" lang="renderjs">
    import html2canvas from 'html2canvas'
    export default {

        methods:{
            // 生成图片需要调用的方法
            onClick(event, ownerInstance) {
                //console.log("22222")
                setTimeout(() => {
                    const dom = document.getElementById('report') // 需要生成图片内容的 dom 节点
                    html2canvas(dom, {
                      width: dom.clientWidth, //dom 原始宽度
                      height: dom.clientHeight,
                      scrollY: 0, // html2canvas默认绘制视图内的页面，需要把scrollY，scrollX设置为0
                      scrollX: 0,
                      useCORS: true, //支持跨域
                      // scale: 2, // 设置生成图片的像素比例，默认是1，如果生成的图片模糊的话可以开启该配置项
                    }).then((canvas) => {
                        // 生成成功
                        console.log("生成成功")
                        // html2canvas 生成成功的图片链接需要转成 base64位的url
                        ownerInstance.callMethod('receiveRenderData', canvas.toDataURL('image/png'))
                    }).catch(err=>{
                        console.log("生成图片失败");
                    })
                }, 300)
            }



        }
    }
</script>
<style lang="scss">
	.report_content {
		width: 80%;
		margin: auto;
		line-height: 30px;

		.report_content_info {
			display: flex;
			// border: 1px solid #555;

			div {
				// border: 1px solid #555;

				flex: 1;
				text-align: left;
			}

			;
		}

		.report_content_details {

			// border: 1px solid #555;
			.details_info {
				display: flex;
				// border: 1px solid #555;

				div {
					flex: 1;
					text-align: left;
					// border: 1px solid #555;

				}

				;
			}
		}
	}


	.particulars {
		width: 80%;
		// margin-top: 24rpx;
		margin: auto;
		// border-radius: 24rpx 24rpx 24rpx 24rpx;
		// 		border: 2rpx solid #74D1E3;

		.particulars-S {
			width: 93%;
			height: 136rpx;
			display: flex;
			justify-content: space-between;

			.top-left {
				width: 303rpx;
				height: 88rpx;
				// margin-left: 24rpx;
				margin-top: 24rpx;
				font-size: 32rpx;
				color: #333333;
			}

			.top-content {
				width: 303rpx;
				height: 88rpx;
				margin-top: 24rpx;

				.a {
					font-size: 32rpx;
					color: #333333;
					margin-bottom: 8rpx;
					font-weight: 700;
				}

				.b {
					font-size: 28rpx;
					color: #AAAAAA;
				}
			}

			.top-right {
				width: 18rpx;
				height: 18rpx;
				border-top: 4rpx solid #AAAAAA;
				border-right: 4rpx solid #AAAAAA;
				transform: rotate(45deg);
				margin-top: 60rpx;
			}
		}

		.particulars-top {
			background-color: #fee0cc;
			border-radius: 24rpx 24rpx 0rpx 0rpx;
		}

	}


	.Reportdetai {
		width: 80%;
		margin: 24rpx auto 0;
		padding-bottom: 24rpx;
		box-sizing: border-box;
		// padding-right: 20rpx;

		.Reportdetai-a {
			// 			width: 686rpx;

			margin-top: 24rpx;
			display: flex;
			// margin-left: 24rpx;

			.Reportdetai-right {
				// padding-right: 40rpx;
				box-sizing: border-box;
				width: 100%;
				font-size: 28rpx;
				color: #666666;

			}
		}


	}

	.downloadButton {
		width: 200px;
		height: 70px;
		line-height: 70px;
		text-align: center;
		margin: auto;
		background: #AAAAAA;
		color: #FFF;
	}
</style>
